import { Login, getUserInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken(),
    userInfo: {}
  },
  mutations: {
    // 设置token
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    removeToken(state) {
      state.token = ''
      removeToken()
    },
    removeUserInfo(state) {
      state.userInfo = {}
    },
    setUserInfo(state, data) {
      state.userInfo = data
    }
  },
  actions: {
    async login(store, data) {
      console.log(27, store)
      const res = await Login(data)
      store.commit('setToken', res.data.jwt)
    },
    async getUserInfo(store) {
      const { data } = await getUserInfo()
      console.log(data)
      store.commit('setUserInfo', data)
    },
    logout(store) {
      store.commit('removeToken')
      store.commit('removeUserInfo')
    }
  }
}
